package panel.media
{
	import assists.HtmlText;
	import assists.Scale9Grid;
	import assists.TextFieldUtils;
	import assists.TimeToStr;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	public class MediaPlayProcess extends Sprite
	{
		private var _loader:ResourceLoader;
		private var _stage:Stage;

		private var _line:Scale9Grid;
		private var _lineBtn:Bitmap;
		private var _timeTxt:TextField;
		private static var LINE_WID:Number;
		private var _lindeWid:Number;
		private var _lineBtnContainer:Sprite;

		private var backGruond:Scale9Grid;
		private var _dragRect:Rectangle;
		private var _total:uint;
		private var _isDrag:Boolean;
		private var _changeProcess:Function;
		public function MediaPlayProcess()
		{
			super();
		}
		
		public function set lindeWid(value:Number):void
		{
			_lindeWid = value;
		}
		
		public function init(loader:ResourceLoader,stage:Stage):void
		{
			_loader = loader;
			_stage = stage;
			_lineBtnContainer = new Sprite();
			layoutUI();
		}
		
		private function layoutUI():void
		{
			var backGroundBit:Bitmap = _loader.uiManager.generateBmp(_loader.uiManager.processLineBack);
			backGruond = new Scale9Grid(backGroundBit.bitmapData,6,6);
			backGruond.mouseEnabled = false;
			backGruond.width = _lindeWid * 0.9;
			backGruond.x = _stage.stageWidth * 0.05;
			this.addChild(backGruond);
			
			
			var lineBit:Bitmap = _loader.uiManager.generateBmp(_loader.uiManager.lineProcess);
			_line = new Scale9Grid(lineBit.bitmapData,4,4);
			_line.mouseEnabled = false;
			_line.width = 1;
			LINE_WID = backGruond.width * 0.94;
			_line.x = backGruond.x + backGruond.width * 0.03;
			_line.y = (backGruond.height - _line.height) * 0.5+ 2;
			this.addChild(_line);
			
			_lineBtn =  _loader.uiManager.generateBmp(_loader.uiManager.lineBtn);
			_lineBtnContainer.addChild(_lineBtn);
			_lineBtn.x = _line.x + _line.width - _lineBtn.width * 0.5;
			
			_timeTxt = new TextField();
			_timeTxt.mouseEnabled = false;
			_timeTxt.autoSize = "left";
			TextFieldUtils.setHtmlText(_timeTxt,HtmlText.formatSizeColor("00:00/00:00",0xffffff));
			_timeTxt.x = backGruond.x + (backGruond.width - _timeTxt.width) * 0.5;
			_timeTxt.y = backGruond.y - _timeTxt.height;
			this.addChild(_timeTxt);
			
			_lineBtnContainer.addEventListener(MouseEvent.MOUSE_DOWN,dragSlideHandler);
			
			this.addChild(_lineBtnContainer);
			
			_dragRect = new Rectangle(backGruond.x - _lineBtn.width * 0.5,0,_lindeWid * 0.8,0);
		}
		
		protected function dragEndHandler(event:MouseEvent):void
		{
			_lineBtnContainer.stopDrag();
			_stage.removeEventListener(MouseEvent.MOUSE_UP,dragEndHandler);
			this.removeEventListener(Event.ENTER_FRAME,frameHandler);
			_isDrag = false;
			_changeProcess(_total *(_lineBtnContainer.x - backGruond.x + _lineBtn.width * 0.5)/(_lindeWid * 0.8));
		}
		
		protected function dragSlideHandler(event:MouseEvent):void
		{
			_isDrag = true;
			_stage.addEventListener(MouseEvent.MOUSE_UP,dragEndHandler);
			_lineBtnContainer.startDrag(false,_dragRect);
			this.addEventListener(Event.ENTER_FRAME,frameHandler);
		}
		
		public function set changeProcss(value:Function):void
		{
			_changeProcess = value;
		}
		
		protected function frameHandler(event:Event):void
		{
			var current:uint = _total *(_lineBtnContainer.x - backGruond.x + _lineBtn.width * 0.5)/(_lindeWid * 0.8);
			update(current,_total,TimeToStr.timeChangeToStr(current) + "/" + TimeToStr.timeChangeToStr(_total));
		}
		
		public function update(current:Number,total:Number,timeStr:String):void
		{
			if(total == 0) return;
			_total = total;
			trace(LINE_WID * current / total);
			_line.width = LINE_WID * current / total;
			if(!_isDrag)
			{
			_lineBtnContainer.x = _line.x + _line.width - _lineBtnContainer.width;
			}
			TextFieldUtils.setHtmlText(_timeTxt,HtmlText.formatSizeColor(timeStr,0xffffff));
		}
		
	}
}